package xcu.lxj.nettychat.chat.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcu.lxj.nettychat.chat.domain.dto.MsgReadInfoDTO;
import xcu.lxj.nettychat.chat.domain.vo.request.*;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMessageReadResp;
import xcu.lxj.nettychat.chat.domain.vo.response.ChatMsgResp;
import xcu.lxj.nettychat.chat.service.inter.ChatService;
import xcu.lxj.nettychat.common.domain.vo.response.ApiResult;
import xcu.lxj.nettychat.common.domain.vo.response.CursorPageBaseResp;
import xcu.lxj.nettychat.common.utils.RequestHolder;

import java.util.Collection;

/**
 * @Author XinJian Lei
 * @Date 2024/9/15 16:21
 * @Version 1.0
 */

@RequestMapping("/chat")
@RestController
@Tag(name = "聊天接口")
@Slf4j
public class ChatController {

    @Autowired
    ChatService chatService;


    @PostMapping("/public/msg/page")
    @Operation(summary = "消息列表")
    public ApiResult<CursorPageBaseResp<ChatMsgResp>> getMsgPage(@RequestBody ChatMessagePageReq request) {
//        log.info("{}",request);
        CursorPageBaseResp<ChatMsgResp> msgPage = chatService.getMsgPage(request, RequestHolder.get().getUid());
        return ApiResult.success(msgPage);
    }


    @Operation(summary = "发送消息")
    @PostMapping("/send")
    public ApiResult<ChatMsgResp> sendMsg(@Valid @RequestBody ChatMsgReq request){
        Long msgId = chatService.sendMsg(request, RequestHolder.get().getUid());
        return ApiResult.success(chatService.getMsgResp(msgId, RequestHolder.get().getUid()));
    }


    @PutMapping("/msg/recall")
    @Operation(summary = "撤回消息 -- 暂未完成")
    public ApiResult<Void> recallMsg(@Valid @RequestBody ChatMessageBaseReq request) {
        chatService.recallMsg(RequestHolder.get().getUid(), request);
        return ApiResult.success();
    }


    @PostMapping("/msg/read/page")
    @Operation(summary = "消息的已读未读列表")
    public ApiResult<CursorPageBaseResp<ChatMessageReadResp>> getReadPage(@RequestBody @Valid ChatMessageReadReq request) {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(chatService.getReadPage(uid, request));
    }


    @PostMapping("/msg/read")
    @Operation(summary = "获取消息的已读未读总数")
    public ApiResult<Collection<MsgReadInfoDTO>> getReadInfo(@RequestBody @Valid ChatMessageReadInfoReq request) {
        Long uid = RequestHolder.get().getUid();
        return ApiResult.success(chatService.getMsgReadInfo(uid, request));
    }


    @PutMapping("/msg/read")
    @Operation(summary = "消息阅读上报")
    public ApiResult<Void> msgRead(@Valid @RequestBody ChatMessageMemberReq request) {
        Long uid = RequestHolder.get().getUid();
        chatService.msgRead(uid, request);
        return ApiResult.success();
    }

}
